package spring.annotations;


import java.lang.reflect.Method;
import java.util.Objects;
import java.util.Set;

public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public Object registerBeanDefinitions(Object... args) {
        if (args.length != 1) {
            throw new AnnotationException("插件注册失败");
        }
        AnnotationInitListener listener = (AnnotationInitListener) args[0];
        Set<Class<?>> allClass = listener.getMapperScanSet();
        allClass.forEach(cls -> {
            Service service = cls.getAnnotation(Service.class);
            if (Objects.isNull(service)) {
                return;
            }
            Transactional transactional = cls.getAnnotation(Transactional.class);
            if (Objects.nonNull(transactional)) {
                listener.getSingletonBeanRegistry().registerTransactionPointCut(cls.toGenericString());
                return;
            }
            Method[] methods = cls.getMethods();
            for (Method method : methods) {
                transactional = method.getAnnotation(Transactional.class);
                if (Objects.isNull(transactional)) {
                    continue;
                }
                listener.getSingletonBeanRegistry().registerTransactionPointCut(method.toGenericString());
            }
        });
        return null;
    }

}
